//
//  APIs.h
//  FishTank
//
//  Created by Scott Zhu on 14/11/26.
//  Copyright (c) 2014年 Scott Zhu. All rights reserved.
//
#define LINUX 1
#import <Foundation/Foundation.h>
#include "PPPP_API.h"
#import "DeviceCtrl.h"
#import "ControlPeriods.h"

#define IGreen             [UIColor colorWithRed:138.0/255.0 green:192.0/255.0 blue:66.0/255.0 alpha:1.0f]
#define CONTROL_QUEUE "controlQueue"
#define VIDEO_QUEUE "videoQueue"

#define SHOW_DEVICE_INFO @"ShowDeviceInfoSegue"
#define SHOW_SMART_CONFIG @"SmartConfigSegue"
#define SHOW_PERIODS_SETTING @"PeriodsSettingSegue"
#define SHOW_DEVICE_EDIT @"ShowDeviceEditSegue"
#define SHOW_ADD_DEVICE @"ShowAddDeviceSegue"

#define MAX_VIDEO_ERROR_CNT   50 //最大视频错误次数0.1秒*50
#define MAX_STATE_ERROR_CNT   30 //最大全局错误次数
#define ERROR_NEED_RECONNECT -23 //需要重新连接

//设备版本，主版本
//add by hebidu 
typedef NS_ENUM(UInt8, DeviceMainVersion)
{
    AQ000 = 0x00, // 设备未知
    AQ107 = 0x17, // 设备挂载AQ107底板
    AQ209 = 0x29, // 设备挂载AQ209底板
    AQ805 = 0x85, // 设备挂载AQ805底板
    AQ211 = 0x21, // 设备挂载AQ211底板
};

typedef NS_ENUM(UInt8, ClientPackType)
{
    CPT_MCU_COMM = 0x01, // 发给单片机的通信包
    CPT_MCU_COMM_FB = 0x02, // 单片机反馈的通信包
    CPT_MJPEG_START = 0x03, // MJPEG视频启动
    CPT_MJPEG_STOP = 0x04, // MJPEG视频停止
    CPT_MJPEG_FRAME = 0x05, // MJPEG视频帧
};


typedef NS_ENUM(UInt8, Mode)
{
    SET = 0,
    READ = 1
};

typedef struct
{
    UInt8 flag;
    ClientPackType type;
    UInt16 length;
}ClientPackHead;

typedef struct
{
    UInt8 devPwd[16];
    Mode mode;
    UInt8 length;
    UInt8 content[2];// 数据内容,如果数据内容只有2字节,则剩余字节不发送,McuData结构体长度也随之变化
}McuData;

typedef struct
{
    UInt8 devPwd[16];
    Mode mode;
    UInt8 length;
    UInt8 content[19];
}PeriodsData;

typedef struct
{
    UInt8 devPwd[16];
    Mode mode;
    UInt8 length;
    Byte content[8];
}SyncTimeData;

typedef struct
{
    UInt8 devPwd[16];
    Mode mode;
    UInt8 length;
    Byte content[3];
}SubsribeTokenData;


typedef struct
{
    UInt8 devPwd[16];
    UInt16 count;// 获取帧数目,设置为2500帧,防止长时间传输损耗流量
    UInt16 frameCycle;// 帧周期,设置为120,则帧周期为120ms
    UInt16 height;// 图像高度,设置为240
    UInt16 width;// 图像宽度,设置为320
}VideoStartData;


typedef struct
{
    UInt8 devPwd[16];
}VideoStopData;

typedef struct
{
    UInt8 devPwd[16];
    Mode mode;
    UInt8 length;
    Byte content[3];
}SetTemperatureData;

typedef struct
{
    ClientPackHead head;
    SetTemperatureData body;
}SetTemperatureRequest;

typedef struct
{
    ClientPackHead head;
    SubsribeTokenData body;
}SubsribeTokenRequest;

typedef struct
{
    ClientPackHead head;
    SyncTimeData body;
}SyncTimeRequest;

typedef struct
{
    ClientPackHead head;
    McuData body;
}ControlRequest;

typedef struct
{
    ClientPackHead head;
    PeriodsData body;
}SetPeriodsRequest;

typedef struct
{
    ClientPackHead head;
    VideoStartData body;
}VideoStartRequest;

typedef struct
{
    ClientPackHead head;
    VideoStopData body;
}VideoStopRequest;

typedef struct
{
    unsigned char hour: 5;
    unsigned char minute: 3;
}TimeStamp;

@interface APIs : NSObject

@property (nonatomic) INT32 currentSession;
@property (atomic) INT32 errorOfSensorCnt; //监听全局状态的错误次数
@property (atomic) INT32 errorOfVideoCnt;//监听视频的错误次数
@property (nonatomic) BOOL isListenOnSensors;
@property (nonatomic) BOOL isListenOnVideo;
@property (nonatomic) BOOL isQuerySubscribeToken;

+ (APIs *)sharedInstance;

/**
 * PPPP——Api 版本
 */
- (NSString *) version;

-(BOOL) checkPPPP;
-(void)reconnect:(void (^)(NSInteger))success
         failure:(void (^)(NSInteger))failure;


- (NSInteger) connectWithUid:(NSString *)uid
             lanSearchEnable:(BOOL)enable
                     success:(void (^)(NSInteger session))success
                     failure:(void (^)(NSInteger errorCode))failure;

- (void) listenOnSensorsWithTimeout: (NSUInteger)timeout
                           password: (NSString *)password
                            updated:(void (^)(float temperature, float ph, Byte controlByte, NSString *lockStatus))updated
                         interupted:(void (^)(NSInteger errorCode))interupted;

- (void) listenOnVideoWithTimeout: (NSUInteger)timeout
                         password: (NSString *)password
                          updated:(void (^)(NSData *data))updated
                       interupted:(void (^)(NSInteger errorCode))interupted;

- (void) controlPeriodWith:(ControlType)controlType
                  password:(NSString *)password
                   success:(void (^)(ControlPeriods *controlPeriods))success
                   failure:(void (^)(NSInteger errorCode))failure;

- (void) setControlPeriod:(ControlPeriods *)controlPeriods
                 password:(NSString *)password
                  success:(void (^)())success
                  failure:(void (^)(NSInteger errorCode))failure;

- (int) sendSensorReadingRequest:(NSString *)password;

- (NSInteger) setLightStatus:(BOOL)lightStatus
                 waterStatus:(BOOL)waterStatus
             disinfectStatus:(BOOL)disinfectStatus
           autoControlStatus:(BOOL)autoControlStatus
                withPassword:(NSString *)password;

- (NSInteger) sendStartVideoRequest:(NSString *)password;
- (NSInteger) sendStopVideoRequest:(NSString *)password;



//设备版本获取
- (void)getDeviceVersionWithpassword:(NSString *)password
                             timeout:(UINT32) timeout
                             success:(void (^)(DeviceMainVersion  version,NSString * mainVersion, NSString * majorVersion))success
                          interupted:(void (^)(NSInteger errorCode))interupted;


//设备锁定与解锁
- (void)deviceLockOrUnlockWithpassword:(NSString *)password
                           LockOStatus:(BOOL)lockStatus
                               success:(void (^)(BOOL status))success
                            interupted:(void (^)(NSInteger errorCode))interupted;

//设备锁定状态获取
- (void)getDeviceLockStatusWithPassword:(NSString *)password
                                success:(void (^)(NSString * lockStatus))success
                             interupted:(void (^)(NSInteger errorCode))interupted;


//水温异常上下限温度设置
- (void)setHighOrLowTemperatureWithHigh:(NSInteger)high
                                    low:(NSInteger)low
                               password:(NSString *)password
                                success:(void (^)())success
                                failure:(void (^)(NSInteger errorCode))failure;


//关闭PPPP
-(void)close;

-(void)close:(INT32) sessionId;

//设置时间
- (void) syncTimeToDevice: (NSString *)password
                  success:(void (^)())success
               interupted:(void (^)(NSInteger errorCode))interupted;

//设置订阅令牌
//-(void) querySubscribeToken: (UINT32)timeout
//                   password: (NSString *)password
//                    success:(void (^)(NSString * temperature , NSString * level, NSString * load))success
//                 interupted:(void (^)(NSInteger errorCode))interupted;

-(void) querySubscribeToken: (UINT32)timeout
                    password: (NSString *)password
                     success:(void (^)(NSString * temperature , NSString * level, NSString * load))success
                 interupted:(void (^)(NSInteger errorCode))interupted;

/**
 * 设置 加热上限温度
 */
-(void) setTemperature:(UINT16 ) temp
              password: (NSString *)password
               success:(void (^)())success
            interupted:(void (^)(NSInteger errorCode))interupted;

@end
